#include "serial.h"
using namespace comm;

void print (char * data, int len) {
	int c;
	
	for (int i=0; i<len;i++){
		c = data [i];
		printf ("%X ", c);
	}
	
	printf ("\n");
}

int main () {	
	CSerialPort * s = new CSerialPort ();
	s->Open ("COM1"); // windows
	//s->Open ("/dev/ttyS0"); // posix
	unsigned int long rc = 0; 
	int len_data_r = 35;
	int len_data_w = 35;
	char * data_r = new char [len_data_r + 1];
	char * data_w = new char [len_data_w + 1];
	for (int i=0; i<len_data_w; i++) data_w [i] = i;
	
	int pos_in_data = 0;
	
	while (1) {
		s->Read (data_r, len_data_r, &rc);		
		if (rc > 0) {
			printf ("[%d] ", (int)(rc));
			print (data_r, rc);
			fflush (stdout);
		}
		memset (data_r, 0, len_data_r);
		
		while (pos_in_data < len_data_w) {
			s->Write (data_w + pos_in_data, 8);
			pos_in_data += 8;
			Sleep (1); // windows
			//sleep (1); // posix
		}
		pos_in_data = 0;
		Sleep (1000); // windows
		//sleep (1000); // posix		
	}
	
	delete data_r;
	delete data_w;
	delete s;
	return 0;
}
